﻿<link href="@Href("~/Styles/Validation.css")" rel="stylesheet" />
@using System.Web.WebPages;
@using Model;
@{

    SiteSecurity.RequireAuthenticatedUser();
    if (Roles.IsUserInRole(UsersRoles.Admin))
    {
        Layout = "~/Admin/_AdminSiteLayout.cshtml";
    }
    else
    {
        Layout = "~/Shared/_SiteLayout.cshtml";
    }

    Page.Title = "Edit Profile";

    Validation.RequireField("readingClass", "You must specify your class.");
    Validation.Add("readingClass", Validator.Range(Question.MinClass, Question.MaxClass, "Class between " + Question.MinClass + " to " + Question.MaxClass));

    bool IsSuccess = false;
    Repository rp = new Repository();
    Profile aProfile = rp.GetProfileByUserId(SiteSecurity.GetUserId());
    if (aProfile == null)
    {
        aProfile = new Profile();
    }

    if (IsPost)
    {
        aProfile.FirstName = Request.Form["FirstName"];
        aProfile.MiddleName = Request.Form["MiddleName"];
        aProfile.LastName = Request.Form["LastName"];
        aProfile.FathersName = Request.Form["FathersName"];
        aProfile.MothersName = Request.Form["MothersName"];
        aProfile.AddressLine1 = Request.Form["AddressLine1"];
        aProfile.AddressLine2 = Request.Form["AddressLine2"];
        if (Request.Form["BirthDate"] != "")
        {
            aProfile.BirthDate = Request.Form["BirthDate"].AsDateTime();
        }
        else
        {
            aProfile.BirthDate = DateTime.Now;
        }
        aProfile.ReadingClass = Request.Form["ReadingClass"].AsInt();
        aProfile.School = Request.Form["School"];
        aProfile.ReadingGroup = Request.Form["ReadingGroup"];
        aProfile.Mobile = Request.Form["Mobile"];
        aProfile.Email = Request.Form["Email"];
        aProfile.FbUserName = Request.Form["FbUserName"];
        aProfile.TwitterUserName = Request.Form["TwitterUserName"];

        IsSuccess = rp.UpDateProfile();

        if (IsSuccess)
        {

    <p class="message success">
        Profile Updated Successfully
    </p>
        }
        else
        {
    <p class="message error">
        There was a problem with your login and/or errors exist in your form.
    </p>
        }
    }
    if (!IsPost)
    {

    }

    <form method="post" action="">
    <fieldset>
        <legend>Edit Pofile</legend>
        <table class="table">
            <tbody>
                @if (IsPost)
                {
                    <tr>
                        <td>
                        </td>
                        <td>
                        </td>
                        <td>
                            <a  class="button medium green" href="@Href("~/Account/Profile")">Back to profile</a>
                        </td>
                    </tr>
                }
                @if (aProfile.UserProfile != null)
                {
                    <tr>
                        <td class="user-info-label">
                            User Name
                        </td>
                        <td>
                            :
                        </td>
                        <td class="user-info">
                            <input type="text" id="@aProfile.UserProfile.Email" name="@aProfile.UserProfile.Email" title="@aProfile.UserProfile.Email" value="@aProfile.UserProfile.Email" readonly="readonly" disabled="disabled" />
                        </td>
                    </tr>
                }
                <tr>
                    <td class="user-info-label">
                        First Name
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="FirstName" name="FirstName" title="@aProfile.FirstName" value="@aProfile.FirstName" />
                    </td>
                </tr>
                <tr>
                    <td class="user-info-label">
                        Mid Name
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="MiddleName" name="MiddleName" title="@aProfile.MiddleName" value="@aProfile.MiddleName" />
                    </td>
                </tr>
                <tr>
                    <td class="user-info-label">
                        Last Name
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="LastName" name="LastName" title="@aProfile.LastName" value="@aProfile.LastName" />
                    </td>
                </tr>
                <tr>
                    <td class="user-info-label">
                        Father's Name
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="FathersName" name="FathersName" title="@aProfile.FathersName" value="@aProfile.FathersName" />
                    </td>
                </tr>
                <tr>
                    <td class="user-info-label">
                        Mother's Name
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="MothersName" name="MothersName" title="@aProfile.MothersName" value="@aProfile.MothersName" />
                    </td>
                </tr>
                <tr>
                    <td class="user-info-label">
                        AddressLine1
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="AddressLine1" name="AddressLine1" title="@aProfile.AddressLine1" value="@aProfile.AddressLine1" />
                    </td>
                </tr>
                <tr>
                    <td class="user-info-label">
                        AddressLine2
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="AddressLine2" name="AddressLine2" title="@aProfile.AddressLine2" value="@aProfile.AddressLine2" />
                    </td>
                </tr>
                <tr>
                    <td class="user-info-label">
                        Birth Day
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="BirthDate" name="BirthDate" title="@aProfile.BirthDate" value="@aProfile.BirthDate" />
                    </td>
                </tr>
                @if (!Roles.IsUserInRole(UsersRoles.Admin))
                {
                    <tr>
                        <td class="user-info-label">
                            Class
                        </td>
                        <td>
                            :
                        </td>
                        <td class="user-info">
                            @* <input type="text" id="ReadingClass" name="ReadingClass" title="@aProfile.ReadingClass" value="@aProfile.ReadingClass" />
                   *@
                            <input type="text" id="readingClass" name="readingClass" title="Classs" value="@aProfile.ReadingClass" placeholder="Class" class="@SiteValidation.GetValidationClass("readingClass")" @Validation.For("readingClass") />
                            @Html.ValidationMessage("readingClass")
                        </td>
                    </tr>
                    <tr>
                        <td class="user-info-label">
                            School
                        </td>
                        <td>
                            :
                        </td>
                        <td class="user-info">
                            <input type="text" id="School" name="School" title="@aProfile.School" value="@aProfile.School" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-info-label">
                            Group
                        </td>
                        <td>
                            :
                        </td>
                        <td class="user-info">
                            <input type="text" id="ReadingGroup" name="ReadingGroup" title="@aProfile.ReadingGroup" value="@aProfile.ReadingGroup" />
                        </td>
                    </tr>
                }
                <tr>
                    <td class="user-info-label">
                        Mobile
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="Mobile" name="Mobile" title="@aProfile.Mobile" value="@aProfile.Mobile" />
                    </td>
                </tr>
                <tr>
                    <td class="user-info-label">
                        Email
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="Email" name="Email" title="@aProfile.Email" value="@aProfile.Email" />
                    </td>
                </tr>
                <tr>
                    <td class="user-info-label">
                        Facebook
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="FbUserName" name="FbUserName" title="@aProfile.FbUserName" value="@aProfile.FbUserName" />
                    </td>
                </tr>
                <tr>
                    <td class="user-info-label">
                        Twitter
                    </td>
                    <td>
                        :
                    </td>
                    <td class="user-info">
                        <input type="text" id="TwitterUserName" name="TwitterUserName" title="@aProfile.TwitterUserName" value="@aProfile.TwitterUserName" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                    </td>
                    <td>
                        <input class="button medium green" type="submit" value="Update" title="Update Profile" />
                    </td>
                </tr>
            </tbody>
        </table>
    </fieldset>
    <script type="text/javascript" src="@Href("~/Scripts/jquery-1.6.2.min.js")"></script>
    <script type="text/javascript" src="@Href("~/Scripts/jquery.validate.min.js")"></script>
    <script type="text/javascript" src="@Href("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
    </form>
}
